package com.atguigu.gmall.rabbit.service;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.rabbit.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    @Qualifier("redis")
    private RedisTemplate redisTemplate;

    public void sendMessage(String exchange,String routeingKey,Object message){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //ID UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        gmallCorrelationData.setId(uuid);
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routeingKey);
        gmallCorrelationData.setMessage(message);

        //将消息发送 Redis中

        redisTemplate.opsForHash().put("rabbit:message",uuid, JSON.toJSONString(gmallCorrelationData));

        rabbitTemplate.convertAndSend(exchange,routeingKey,message,gmallCorrelationData);
    }
}
